#include "DebugRenderer.h"
#include "..\utils\Timer.h"
#include "..\utils\Profiler.h"

namespace ds {

DebugRenderer::DebugRenderer() {
	//if ( !gEngine->getResourceManager().contains("Verdana_12") ) {
		gEngine->getResourceManager().loadFont("Verdana_12","Verdana",12,false);
	//}
	IDirect3DDevice9 * pDevice = gEngine->getDevice();
	HR(D3DXCreateSprite( pDevice, &m_Sprite ));
}


DebugRenderer::~DebugRenderer() {
	SAFE_RELEASE(m_Sprite);
}

void DebugRenderer::clear() {
	m_TextLines.clear();
	for ( size_t i = 0; i < m_Meshes.size(); ++i ) {
		SAFE_RELEASE(m_Meshes[i]);
	}
	m_Meshes.clear();
}

void DebugRenderer::addSphere(const Vec3& pos) {
	LPD3DXMESH meshBox;
	HR(D3DXCreateSphere(
		gEngine->getDevice(),    
		10.0f, // radius
		10, // Slices
		10, // Stacks
		&meshBox,
		0));
	m_Meshes.push_back(meshBox);
	m_MeshPositions.push_back(pos);
}

void DebugRenderer::draw() {
	IDirect3DDevice9 * pDevice = gEngine->getDevice();
	ID3DXFont *font = gEngine->getResourceManager().getFont("Verdana_12");	
	if ( font != 0 ) {
		RECT font_rect;	
		m_Sprite->Begin( D3DXSPRITE_ALPHABLEND );
		for ( size_t i = 0; i < m_TextLines.size(); ++i ) {	
			TextLine *tl = &m_TextLines[i];
			//FIXME: calculate rect correctly
			int xp = (int)tl->pos.x;
			int yp = (int)tl->pos.y;
			SetRect(&font_rect,xp,yp,xp+200,yp+60);		
			font->DrawTextA(m_Sprite,tl->message.c_str(),-1,&font_rect,DT_LEFT|DT_NOCLIP,tl->color);		
		}
		m_Sprite->End();
	}
	/*
	D3DXMATRIX matrix;
	gEngine->getRenderer()->set2DCameraOn();
	gEngine->getRenderer()->setRenderState( D3DRS_LIGHTING,false);
	//gEngine->getRenderer()->setRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	for ( size_t i = 0; i < m_Meshes.size(); ++i ) {
		Vec3 pos = m_MeshPositions[i];
		D3DXMatrixTranslation(&matrix,  pos.x, pos.y,  pos.z);
		gEngine->getDevice()->SetTransform(D3DTS_WORLD, &matrix);
		m_Meshes[i]->DrawSubset(0);
	}
	gEngine->getRenderer()->setRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	gEngine->getRenderer()->set2DCameraOff();
	*/
}

void DebugRenderer::showDrawCounter(const Vec2& pos) {
	addText(pos,ds::Color(1.0f,1.0f,1.0f,1.0f),"DrawCounter IDX %d Vertices %d Sprites %d",gEngine->getDrawCounter()->getIndexCounter(),gEngine->getDrawCounter()->getPrimitiveCounter(),gEngine->getDrawCounter()->getSpriteCounter());
}

void DebugRenderer::showFPS(const Vec2& pos) {
	ds::CTimer* timer = dynamic_cast<ds::CTimer*>(gEngine->getService("FPSTimer"));
	if ( timer != NULL ) {
		addText(pos,ds::Color(1.0f,1.0f,1.0f,1.0f),"FPS %.0f",timer->getFPS());	
	}
}

void DebugRenderer::showProfile(const Vec2& pos) {
	gProfiler->show(pos,this);
}

void DebugRenderer::addText(const Vec2& pos,char* format,...) {
	va_list args;
	va_start(args,format);
	addText(pos,Color(1.0f,1.0f,1.0f,1.0f),format,args);
	va_end(args);
}

void DebugRenderer::addText(const Vec2& pos,char* format,va_list args) {
	addText(pos,Color(1.0f,1.0f,1.0f,1.0f),format,args);
}

void DebugRenderer::addText(const Vec2& pos,const Color& color,char* format,...) {
	va_list args;
	va_start(args,format);
	addText(pos,color,format,args);
	va_end(args);
}

void DebugRenderer::addText(const Vec2& pos,const Color& color,char* format,va_list args) {
	TextLine tl;
	char buffer[1024];
	memset(buffer, 0, sizeof(buffer));
	int written = vsnprintf_s(buffer,sizeof(buffer),_TRUNCATE,format,args);		
	tl.message = std::string(buffer);
	tl.pos = pos;
	tl.color = color;
	m_TextLines.push_back(tl);
}

void DebugRenderer::addText(const Vec2& pos,const char* text) {
	TextLine tl;	
	tl.message = std::string(text);
	tl.pos = pos;
	tl.color = Color(1.0f,1.0f,1.0f,1.0f);
	m_TextLines.push_back(tl);
}

}
